﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SanPham_Product : System.Web.UI.Page
{
    static DataTable tbPro = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Username"] != null)
        {
            btnRedirect.Visible = true;
            BtnLogin.Visible = false;
            BtnLogout.Visible = true;
            BtnOrder.Visible = true;
        }
        else
        {
            BtnOrder.Visible = false;
            btnRedirect.Visible = false;
            BtnLogin.Visible = true;
            BtnLogout.Visible = false;
        }


        if (!IsPostBack)
        {
            if (Session["ShoppingCart"] != null)
            {
                tbPro = Session["ShoppingCart"] as DataTable;
            }
            else
            {
                tbPro.Rows.Clear();
                tbPro.Columns.Clear();
                tbPro.Columns.Add("PID", typeof(string));
                tbPro.Columns.Add("Name", typeof(string));
                tbPro.Columns.Add("Price", typeof(string));
                tbPro.Columns.Add("Quantity", typeof(int));
                //tbPro.Columns.Add("Sum", typeof(int));
            }
        }
    }

    protected void tbProduct_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string id = tbProduct.Rows[e.NewSelectedIndex].Cells[1].Text;
        string name = tbProduct.Rows[e.NewSelectedIndex].Cells[2].Text;
        string price = tbProduct.Rows[e.NewSelectedIndex].Cells[3].Text;
        int quantity = 1;
       
        //Add vao gio hang

        foreach (DataRow row in tbPro.Rows)
        {//Kiem tr neu mat hang da co roi thi tang so luong len 1
            if (row["PID"].ToString() == id)
            {
                row["Quantity"] = (int)row["Quantity"] + 1;
                goto ShoppingCart;
            }
        }
        tbPro.Rows.Add(id, name, price, quantity);
    ShoppingCart:
        Session["ShoppingCart"] = tbPro;

        Response.Write("<script>alert('Your product has been added to the cart!...')</script>");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Product/ShoppingCart.aspx");
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Login/Login.aspx");

    }
    protected void BtnLogout_Click1(object sender, EventArgs e)
    {
        Session["Username"] = null;
        Response.Redirect("../Login/Login.aspx");
    }
    protected void BtnOrder_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Order/Default.aspx");
    }
    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        clsLogIn a = (clsLogIn)(Session["Username"]);

        if (a.IsAdmin)
        {
            Response.Redirect("../User Information/Information.aspx");
        }
        else
        {
            Response.Redirect("../Add Products/AddProducts.aspx");
        }
    }
}